<?php
	function getdata($query){
		$data=mysql_query($query) or die(mysql_error());
		return $data;
	}
	for($i=1;$i<=3;$i++)
	{
		switch($i){
			case 1:$select="select * from phim where linkanh is not null order by maphim DESC limit 0,5";$ps_title="Phim mới đăng";$link="index.php?key=new";$ps_class="ps_icon_3";break;
			case 2:$select="select * from phim where linkanh is not null order by view DESC limit 0,5";$link="index.php?key=request";$ps_title="Phim theo yêu cầu";$ps_class="ps_icon_4";break;
			case 3:$select="select * from phim where linkanh is not null order by ngaydang DESC limit 0,5";$link="index.php?key=mostviewed";$ps_title="Phim hay";$ps_class="ps_icon_2";break;
		}?>		
        <div class="boxes">
            <h2 class="ps_title"><span class="<?php echo $ps_class?>"></span>
                <a href="<?php echo $link?>" class="ps_title"><?php echo $ps_title?></a>
            </h2>
            <div class="list">
            <?php
			$data = getdata($select);
			$number=1;
            while($d=mysql_fetch_array($data))
            {?>
                <div class="item">
                    <span class="number"><?php echo $number;$number ++;?></span>
                    <a class="no_title" href="index.php?key=detail&id=<?php echo $d['maphim']?>&maloai=<?php echo $d['maloai']?>"><?php echo $d['tenphim']?></a><br>
                    <div class="star">
                    	<?php
						$rating = mysql_query("select AVG(diem) from rate,thongtinphim where rate.marate=thongtinphim.marate and thongtinphim.maphim = {$d['maphim']}");
						$r=mysql_fetch_array($rating);
						$diem=$r['AVG(diem)'];
						for($j=1;$j<=5;$j++)
						{
							if($j<$diem)
							{
								echo "<span class='star_on_png'></span>";
							}
							else
							{ 
								if(($j-0.5)<$diem)
									echo "<span class='star_half_png'></span>";
								else 
									echo "<span class='star_off_png'></span>";
							}
						}
						?>
                    </div>
                </div>    
            <?php }?>      
            <a class="readmore no_title" href="<?php echo $link ?>">Xem tất cả <?php echo $ps_title ?> »</a>  
            </div>
        </div><div class="clear"></div><br /><br /><!-- end of each box -->
        <?php 
}?>
